<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Model;

/**
 * Description of BookCateModel
 *
 * @author gangguo
 */
class BookCateModel extends XuecheModel{
    //put your code here
    protected $pk = 'cate_id';
    protected $tableName = 'book_cate';
    protected $fields = array(
            'cate_id', 'cate_name','alias','tj_book','is_view'
        );

    protected $_useCache = true;
    /**
     * 清空数据缓存
     *
     */
    public function ClearAllCache() {

    }
    public function Validate($data) {
        if(empty($data['cate_name'])){
            return FALSE;
        }
        return FALSE;
    }

    //根据ID 回去类名
    public function getCate($cate_id,$field){
        if($cate_id > 0){
            $info = $this->where("`$this->pk` = '{$cate_id}'")->field($field)->select();
            $cate = self::showName($info);
            return $cate[0];
        }
    }

    public function getList($where = '', $order = '', $offset = 0, $limit = 18,$fields = '',$total=FALSE){
        if(empty($where)){
            $where = 'disabled=1';
        }else{
            $where .= ' AND disabled=1';
        }
        $info = parent::getList($where, $order, $offset, $limit,$fields,$total);
        $info['lists'] = self::showName($info['lists']);
        return $info;
    }

    //检查分类名字有没有重复，没有添加新分类，最后都返回类名的ID
    public function getCateId($cate_name){
        if(empty($cate_name)) return FALSE;
        $id = $this->where("`cate_name` = '{$cate_name}' OR `alias` = '{$cate_name}'")->field('cate_id')->find();
        if($id['cate_id'] > 0){
            return $id['cate_id'];
        }else{
            $data = array('cate_name'=>$cate_name);
            return $this->add($data);
        }
    }

    public function saveCate($data){
        if($this->Validate($data)){
            if(empty($data[$this->pk])){
                return $this->add($data);
            }else{
                return $this->save($data);
            }
        }
        return FALSE;
    }

    public function updateAlias($cate_id,$alias){
        if(empty($cate_id) || mb_strlen($alias) > 15){            return FALSE;}
        $data['alias'] = $alias;
        return $this->where("`{$this->pk}` = '{$cate_id}'")->save($data);
    }

    //获取导航菜单数据
    static public function getNavigation($num = 6){
        $obj = new self();
        $info = $obj->getList("`is_view`='2'");
        return $info['lists'];
    }

    //获取分类的显示名字
    static public function showName($info){
        if(is_array($info)){
            foreach ($info as $key => $value) {
                # code...
                if(empty($value['alias'])){
                    $info[$key]['name'] = $value['cate_name'].'小说';
                }else{
                    $info[$key]['name'] = $value['alias'];
                }
            }
        }
        return $info;
    }

    //获取所有分类的数组 cate_id=>cate_name
    public function getCateArray(){
        $cates = self::showName($this->getList());
        $data = array();
        foreach ($cates['lists'] as $value) {
            $data[$value[$this->pk]] = $value['name'];
        }
        return $data;
    }
}
